//
//  QDStationDetailInfoHeaderView.swift
//  QDMetroClient
//
//  Created by YueWen on 2019/3/3.
//  Copyright © 2019 YueWen. All rights reserved.
//

import UIKit

protocol QDStationDetailInfoHeaderViewDelegate: class {
    
    /// 设置为进站或者出站的回调
    func stationDetailInfoHeaderView(view: QDStationDetailInfoHeaderView, clickType:QDStationDetailInfoHeaderView.ClickType)
}


extension QDStationDetailInfoHeaderViewDelegate {
    
    func stationDetailInfoHeaderView(view: QDStationDetailInfoHeaderView, clickType:QDStationDetailInfoHeaderView.ClickType){}
}

/// 顶部放置站点名称以及选择为起点或者终点的视图
final class QDStationDetailInfoHeaderView: UIView {

    /// 点击类型
    enum ClickType {
        /// 进站点击
        case start
        /// 出站点击
        case end
    }
    
    /// 是否响应选择，默认为false
    var isEdit: Bool  = false {
        willSet {
            if !newValue { return }
            startButton.isHidden = false
            endButton.isHidden = false
            lineView.isHidden = false
        }
    }
    
    /// 当前高度
    var height: CGFloat {
        get {
            return 100 + 0.5 + BarDistance.NavigationBar.safeDistance + CGFloat(isEdit ? 40 : 0)
        }
    }
    
    /// 代理对象
    weak var delegate: QDStationDetailInfoHeaderViewDelegate?
    
    /// 背景图
    let bgImageView = UIImageView()
    /// 名字
    let nameButton = UIButton()
    /// 设为起点
    lazy private var startButton: UIButton = {
       
        let button = UIButton()
        
        button.adjustsImageWhenHighlighted = false
        button.titleLabel?.font = Font.regular.font(size: 14)
        button.setTitle("设为起点", for: .normal)
        button.setTitleColor(QDColors.theme, for: .normal)
        button.setImage("metro_station_start".namedImage, for: .normal)
        button.titleEdgeInsets.left = 10
        
        self.addSubview(button)
        
        button.snp.makeConstraints({ (make) in
            make.width.equalTo(UIScreen.main.ritl_width / 2 - 0.25)
            make.top.equalTo(self.bgImageView.snp.bottom)
            make.height.equalTo(40)
            make.left.equalToSuperview()
        })
        
        button.addTarget(self, action: #selector(typeButtonDidTap(sender:)), for: .touchUpInside)
        
        return button
    }()
    /// 设为终点
    lazy private var endButton: UIButton = {
        
        let button = UIButton()
        
        button.adjustsImageWhenHighlighted = false
        button.titleLabel?.font = Font.regular.font(size: 14)
        button.setTitle("设为终点", for: .normal)
        button.setTitleColor(#colorLiteral(red: 0.9803921569, green: 0.4784313725, blue: 0.4784313725, alpha: 1), for: .normal)
        button.setImage("metro_station_end".namedImage, for: .normal)
        button.titleEdgeInsets.left = 10
        
        self.addSubview(button)
        
        button.snp.makeConstraints({ (make) in
            make.width.equalTo(UIScreen.main.ritl_width / 2 - 0.25)
            make.top.equalTo(self.bgImageView.snp.bottom)
            make.height.equalTo(40)
            make.right.equalToSuperview()
        })
        
        button.addTarget(self, action: #selector(typeButtonDidTap(sender:)), for: .touchUpInside)
        
        return button
    }()
    /// 分割线
    lazy var lineView: UIView = {
       
        let view = UIView()
        view.backgroundColor = #colorLiteral(red: 0.9294117647, green: 0.9411764706, blue: 0.9333333333, alpha: 1)
        
        self.addSubview(view)
        
        view.snp.makeConstraints({ (make) in
            make.width.equalTo(0.8)
            make.centerY.equalTo(self.endButton)
            make.height.equalTo(26)
            make.centerX.equalToSuperview()
        })
        
        return view
    }()
    /// 返回按钮
    let backButton = UIButton()
    /// 底部的模拟虚线
    private let bottomView = UIView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        bgImageView.image = "metro_station_bg".namedImage
        bottomView.backgroundColor = #colorLiteral(red: 0.9294117647, green: 0.9411764706, blue: 0.9333333333, alpha: 1)
        
        nameButton.contentHorizontalAlignment = .left
        nameButton.titleEdgeInsets.left = 5
        nameButton.setTitle("李村站", for: .normal)
        nameButton.titleLabel?.font = Font.regular.font(size: 22)
        nameButton.setTitleColor(.white, for: .normal)
        nameButton.setImage("common_btn_back".namedImage, for: .normal)
        
        addSubview(bgImageView)
        addSubview(nameButton)
        addSubview(bottomView)
    }
    
    /// 用于设置完nameButton后进行布局调用
    func buildLayouts(){
        
        bgImageView.snp.makeConstraints { (make) in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(100 + BarDistance.NavigationBar.safeDistance)
        }
        
        nameButton.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.height.equalTo(30)
            make.width.equalTo(nameButton.intrinsicContentSize.width + 5)
            make.top.equalToSuperview().offset(50 + BarDistance.NavigationBar.safeDistance)
        }
        
        bottomView.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview()
            make.left.equalToSuperview().offset(0.5)
            make.right.equalToSuperview().inset(0.5).priority(.high)
            make.height.equalTo(0.5)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 设置为进站\出站按钮点击
    @objc func typeButtonDidTap(sender: UIButton){
        delegate?.stationDetailInfoHeaderView(view: self, clickType: sender == startButton ? .start : .end)
    }
}
